$(function () {
	//补零函数
	function padZero(n) {
		if (n < 10) return "0" + n;
		else {
			return n;
		}
	}

	//获取新闻列表

	function getNewsList() {
		$.ajax({
			method: "GET",
			url: "http://www.liulongbin.top:3006/api/news",
			success: function (res) {
				if (res.status !== 200) {
					return alert("获取新闻失败");
				}
				//处理标签数组
				for (var i = 0; i < res.data.length; i++) {
					res.data[i].tags = res.data[i].tags.split(",");
				}

				//处理时间
				template.defaults.imports.dateFormat = function (dtStr) {
					var date = new Date(dtStr);
					var y = date.getFullYear(); //获取年
					var m = padZero(date.getMonth() + 1); //获取月
					var d = padZero(date.getDate()); //获取日期
					var hh = padZero(date.getHours());
					var mm = padZero(date.getMinutes());
					var ss = padZero(date.getSeconds());
					return y + "-" + m + "-" + d + " " + hh + ":" + mm + ":" + ss;
				};

				console.log(res);
				//4.调用template函数
				var htmlstr = template("tpl-user", res);
				console.log(htmlstr);

				//5.渲染HTML结构
				$("#news-list").html(htmlstr);
			},
		});
	}
	getNewsList();
});
